﻿//set main namespace
goog.provide('WeaponFactory');

goog.require('goog.events.EventType');

goog.require('lime.fill.Stroke');
goog.require('lime.GlossyButton');

goog.require('GLL');
goog.require('GLL.Rect');

'use strict';

//格子系统
/*
var arg = {
	Receiver : object,
	FiledSize : 1024,
	cnt : 16,
	posX : 0,
	posY : 0,
	fill : lime.fill.Fill,
	stroke : lime.fill.Stroke
}
*/
GridSystem = function (grid)
{
	this._grids = [];
	this._gridSize = grid.filedSize / grid.count;
	this._gridCnt = grid.count;

	this._evtRecr = new lime.Sprite().setAnchorPoint(0, 0).setPosition(grid.posX, grid.posY).setSize(grid.filedSize, grid.filedSize);
	GLL.addToScene(this._evtRecr);

	var THIS = this;
	goog.events.listen(this._evtRecr, GLL.EVENT_MD, function (e)
	{
		THIS._btnDn = true;
		var target = THIS.GetGrid(e.position);
		if (!target) return;
		grid.receiver.GridSystemEvent(GLL.Evt.TOUCH,THIS, target);
	});

	goog.events.listen(this._evtRecr, GLL.EVENT_MU, function (e)
	{
		THIS._btnDn = null;
		var target = THIS.GetGrid(e.position);
		if (!target) return;
		grid.receiver.GridSystemEvent(GLL.Evt.LEFT,THIS, target);
	});

	goog.events.listen(this._evtRecr, GLL.EVENT_MM, function (e)
	{
		if(!THIS._btnDn) return;
		var target = THIS.GetGrid(e.position);
		if (!target) return;
		grid.receiver.GridSystemEvent(GLL.Evt.MOVE, THIS, target);
	});

	var gridHalfSize = this._gridSize / 2;

	for (var x = 0; x < grid.count; x++)
	{
		var posX = x * this._gridSize + gridHalfSize;
		this._grids[x] = [];
		for (var y = 0; y < grid.count; y++)
		{
			var posY = y * this._gridSize + gridHalfSize;
			var rect = new GLL.Rect(this._evtRecr).setPosition(posX, posY).setSize(this._gridSize, this._gridSize)
				.setStroke(grid.stroke).setFill(grid.fill);
			rect.x = x;
			rect.y = y;
			this._grids[x][y] = rect;
		}
	}
}

//通过坐标得到格子
GridSystem.prototype.GetGrid = function(pos)
{
	var x = Math.ceil(pos.x / this._gridSize) - 1;
	if (x >= this._gridCnt) return null;
	var y = Math.ceil(pos.y / this._gridSize) - 1;
	if (y >= this._gridCnt) return null;
	var target = this._grids[x][y];
	return target;
}

//-------------------------------------------------------------
WeaponFactory = function ()
{
	//这里的索引对应的元素关系，永远不变
	this._gridFillFix = [
		new lime.fill.Color(''), //空白0
		GLL.getImageFill('ElemErase'), // 1 擦除
		GLL.getImageFill('ElemLeser'), // 2 发射 激光 
		GLL.getImageFill('ElemFire'), // 3 发射 子弹
		GLL.getImageFill('ElemExit'), // 4 出口

		//传导管
		new lime.fill.Color('#FFFFFF'),//5 x
		new lime.fill.Color('#C0C0C0'),//6
		new lime.fill.Color('#808080'),
		new lime.fill.Color('#404040'),
		new lime.fill.Color('#000000'),//9 x
		
		new lime.fill.Color('#FF0000'),//10 x
		new lime.fill.Color('#C00000'),
		new lime.fill.Color('#800000'),
		new lime.fill.Color('#440000'),
		
		new lime.fill.Color('#FFFF00'),//14
		new lime.fill.Color('#C0C000'),
		new lime.fill.Color('#808000'),
		new lime.fill.Color('#404000'),
		
		new lime.fill.Color('#00FF00'),//18
		new lime.fill.Color('#00C000'),
		new lime.fill.Color('#008000'),
		new lime.fill.Color('#004000'),
		
		new lime.fill.Color('#00FFFF'),//22
		new lime.fill.Color('#00C0C0'),
		new lime.fill.Color('#008080'),
		new lime.fill.Color('#004040'),
		
		new lime.fill.Color('#0000FF'),//26
		new lime.fill.Color('#0000C0'),
		new lime.fill.Color('#000080'),
		new lime.fill.Color('#000040'),
		
		new lime.fill.Color('#FF00FF'),//30
		new lime.fill.Color('#C000C0'),
		new lime.fill.Color('#800080'),
		new lime.fill.Color('#400040'),
		
		null
	];
	
	//特殊组件 特殊处理
	this._gridFillFix[0].str = "#FFFFFF";
	this._gridFillFix[1].str = "#FFFFFF";
	this._gridFillFix[2].str = "#FF7F00";
	this._gridFillFix[3].str = "#FF7F00";
	this._gridFillFix[4].str = "#FF0000";
	
	//特殊组件 特殊处理
	for( var i = 0; i < this._gridFillFix.length-1; i++)
	{
		this._gridFillFix[i]._fillFixIdx = i;
	}
	
	this._SelCnt = 6;	
	this._GridCnt = 16 + 1;
	this._frame = new lime.fill.Stroke(1, '#CCC');
	this._frameHL = new lime.fill.Stroke(4, '#F00');
	this._frameAP = new lime.fill.Stroke(4, '#000');
	this._gridLocked = new lime.fill.Color('#D8D8D8');
}

WeaponFactory.prototype.Init = function ()
{
	GLL.init();
	GLL.Game.Init();
	GLL.newScene('mod_std');
	GLL.useScene('mod_std');
	document.title = "Weapon Factory";

	this._gridFill = [];
	//特殊元素
	this._gridFill[0] = this._gridFillFix[1];
	this._gridFill[1] = this._gridFillFix[2];
	this._gridFill[2] = this._gridFillFix[3];
	this._gridFill[3] = this._gridFillFix[4];

	for( var i = 6; i < this._gridFillFix.length-1; i++)
	{
		if(i == 10 || i == 9) continue;
		this._gridFill.push(this._gridFillFix[i]);
	}
	
	this._gridFill.push(this._gridFillFix[0]);
	this._gridBlank = this._gridFillFix[0];
	this._fillSel = 2;
}

WeaponFactory.prototype.GridSystemEvent = function (evt, grid, target)
{
	if (grid == this._Grid)
	{
		this.GridEvent(target);
	}
	
	if (grid == this._Sel)
	{
		this.SelTouch(target);
	}
}

//选定画笔
WeaponFactory.prototype.SelTouch = function (target)
{
	if(this._lastSel == target) return;
	var selIdx = target.y * this._SelCnt + target.x;
	if(selIdx >= this._gridFill.length) return;
	this._fillSel = this._gridFill[selIdx]._fillFixIdx;

	target.setStroke(this._frameHL);
	if(this._lastSel){
		this._lastSel.setStroke(this._frame);
	}
	this._lastSel = target;
}

//设置格子颜色
WeaponFactory.prototype.GridEvent = function (target)
{
	//锁定的
	if(target._GridType == null) return 0;
	//第0个格子是橡皮
	var erasing= (this._fillSel == 1 );
	if(erasing){
		target._GridType = 0; //0 1 都是空白
		target.setFill(this._gridBlank);
		this.RefreshGrid_remove(target.x, target.y);
	}else{
		target._GridType = this._fillSel;
		target.setFill(this._gridFillFix[this._fillSel]);
		this.RefreshGrid_put(target.x, target.y);
	}

}


WeaponFactory.InitElemData = function(pair)
{
	var len = pair.val.length;

}

var SaveValueResult = function(desc)
{
	GLL.Game.LoginName;
	var str = desc;
	GLL.Game.GetKey('ARG', function(pair){WeaponFactory.InitElemData(pair)});
}

WeaponFactory.prototype.OKBtnPushed = function()
{	
	//Send Color To Server
	var imgData = '';
	var imgStr = '[';
	
	for(var y = 0; y < this._GridCnt; y++)
	for(var x = 0; x < this._GridCnt; x++)
	{
		var sprt = this._Grid._grids[x][y];
		var type = sprt._GridType;
		if(!type) type= 0;
		imgStr += type + ', ';
		imgData += this._gridFillFix[type].str.substring(1);
	}

	imgStr += 'null ]';
	
	GLL.Game.SaveImg(imgData, 100);
	GLL.Game.SaveKeyVal('ARG', imgStr, function(str){SaveValueResult(str)});
	
}


//初始化格子系统 宽度的2/3
WeaponFactory.prototype.InitGrid = function ()
{
	//元素拼装格子
	var filedSz = GLL.Rsltn.w * 2 / 3;
	var GridArg = 
	{
		receiver : this,
		//宽度的2/3
		filedSize : filedSz,
		count : this._GridCnt,
		posX : (GLL.Rsltn.h - filedSz) / 2,
		posY : (GLL.Rsltn.h - filedSz) / 2,
		fill : this._gridBlank,
		stroke : this._frame
	}

	this._Grid = new GridSystem(GridArg);
	this._gridMid = Math.floor(GridArg.count / 2);
	this._Grid._grids[this._gridMid][this._gridMid].setStroke(this._frameAP);
	this.InitAllGridType();
	this.DisableAllGrid();

	//元素选择格子
	var selGap = GridArg.posX;
	var posX = GridArg.posX + GridArg.filedSize + selGap;
	var SelArg = 
	{
		receiver : this,
		filedSize : GLL.Rsltn.w - posX - selGap,
		count : this._SelCnt,
		posX : posX,
		posY : GridArg.posY,
		fill : this._gridBlank,
		stroke : this._frame
	}
	
	this._Sel = new GridSystem(SelArg);
	this.SetGrids();
	
	//确定按钮
	var btnW = SelArg.filedSize;
	var btnH = 40;
	var btnY = GridArg.posY + SelArg.filedSize + selGap;
	var btnX = posX;
	var btn = new lime.GlossyButton('OK').setSize(btnW, btnH).setPosition(posX + btnW/2, btnY + btnH/2);
	var THIS = this;
    goog.events.listen(btn, 'click', function(){
		THIS.OKBtnPushed();
    });
	GLL.addToScene (btn);
}

//填充 Sel 面板的颜色
WeaponFactory.prototype.SetGrids = function()
{
	var idx = 0;
	for(var y = 0; y < this._SelCnt; y++)
	for(var x = 0; x < this._SelCnt; x++)
	{
		if(idx >= this._gridFill.length) break;
		this._Sel._grids[x][y].setFill(this._gridFill[idx]);
		idx++;
	}
	//默认选择
	this._lastSel = this._Sel._grids[1][0];
	this._lastSel.setStroke(this._frameHL);
	this._fillSel = 2;
}

//信息显示，完成按钮
WeaponFactory.prototype.InitConsole = function()
{
	
}

WeaponFactory.prototype.InitAllGridType = function()
{
	for(var y = 0; y < this._GridCnt; y++)
	for(var x = 0; x < this._GridCnt; x++)
	{
		var sprt = this._Grid._grids[x][y];
		sprt._GridType = 0;
	}
}


WeaponFactory.prototype.DisableAllGrid = function()
{
	for(var y = 0; y < this._GridCnt; y++)
	for(var x = 0; x < this._GridCnt; x++)
	{
		if(y == this._gridMid && x == this._gridMid){
			continue;
		}
		var sprt = this._Grid._grids[x][y];
		if(sprt._GridType == null) continue;
		if(sprt._GridType == 0){
			//lock it
			sprt.setFill(this._gridLocked);
			sprt._GridType = null;
		}
	}
}

//对 AP 和 色彩 无效
WeaponFactory.prototype.EnableAGrid = function(x, y, ena)
{
	if(y < 0 || y >= this._GridCnt) return;
	if(x < 0 || x >= this._GridCnt) return;
	var sprt = this._Grid._grids[x][y];
	
	if( sprt._GridType  && sprt._GridType > 0)
	{
		return
	}
	
	if(ena || (y == this._gridMid && x == this._gridMid)){
		sprt.setFill(this._gridBlank);
		sprt._GridType = 0;
	}else{
		sprt.setFill(this._gridLocked);
		sprt._GridType = null;
	}
}

//不可以放置的格子暗下去
//只能放置在相邻的格子里。第一个格子，必须放置在固定点
WeaponFactory.prototype.RefreshGrid_put = function(x, y)
{
	var sprt = this._Grid._grids[x][y];
	//有颜色的才检查
	this.EnableAGrid(x, y-1, true);
	this.EnableAGrid(x, y+1, true);
	this.EnableAGrid(x-1, y, true);
	this.EnableAGrid(x+1, y, true);
}

//移出一个格子的时候，需要检测周围的情况
WeaponFactory.prototype.RefreshGrid_remove = function(x, y)
{
	var TL, BR, TR, BL, T2, L2, B2, R2, T, L, B, R;
	var maxIdx = this._GridCnt - 1;
	
	var xa1ok = (x + 1 <= maxIdx);
	var xm1ok = (x -1 >= 0);
	var ya1ok = ( y + 1 <= maxIdx);
	var ym1ok = (y -1 >= 0);
	
	if(xm1ok)
	{
		L = this._Grid._grids[x-1][y]._GridType;
		
		if(ym1ok)
			TL = this._Grid._grids[x-1][y-1]._GridType;
		if(ya1ok)
			BL = this._Grid._grids[x-1][y+1]._GridType;
	}
	
	if(xa1ok)
	{
		R = this._Grid._grids[x+1][y]._GridType;
		
		if(ym1ok)
			TR = this._Grid._grids[x+1][y-1]._GridType;
		if(ya1ok)
			BR = this._Grid._grids[x+1][y+1]._GridType;
	}
	
	if(ym1ok)
		T = this._Grid._grids[x][y-1]._GridType;
	if(ya1ok)
		B = this._Grid._grids[x][y+1]._GridType;

	var xa2ok = (x + 2 <= maxIdx);
	var xm2ok = (x - 2 >= 0);
	var ya2ok = ( y + 2 <= maxIdx);
	var ym2ok = (y - 2 >= 0);

	if(ym2ok)
		T2 = this._Grid._grids[x][y-2]._GridType;
	if(xm2ok)
		L2 = this._Grid._grids[x-2][y]._GridType;
	if(ya2ok)
		B2 = this._Grid._grids[x][y+2]._GridType;
	if(xa2ok)
		R2 = this._Grid._grids[x+2][y]._GridType;
	
	//Top Cell:
	if(!T){
		if(!(T2>0 || TL >0 || TR>0)){
			this.EnableAGrid(x, y-1, false);
		}
	}
	
	if(!L){
		if(!(L2>0 || TL >0 || BL>0)){
			this.EnableAGrid(x-1, y, false);
		}
	}
	
	if(!B){
		if(!(B2>0 || BL>0 || BR >0)){
			this.EnableAGrid(x, y+1, false);
		}
	}

	if(!R){
		if(!(R2>0 || BR >0 || TR>0 )){
			this.EnableAGrid(x+1, y, false);
		}
	}
	
	if(T || L || B || R){
		this.EnableAGrid(x, y, true);
	}else{
		this.EnableAGrid(x, y, false);
	}

}

WeaponFactory.prototype.CheckGrid = function()
{
	
	

}

/*
计算武器的数据
发射基数值 A
传导管增加 A 的速度和威力
出口平分 A
*/
WeaponFactory.prototype.CalData = function()
{

}

// entrypoint
WeaponFactory.start = function ()
{
	var wf = new WeaponFactory();
	wf.Init();
	wf.InitGrid();
}

//this is required for outside access after code is compiled in ADVANCED_COMPILATIONS mode
goog.exportSymbol('WeaponFactory.start', WeaponFactory.start);
